import { HunterTool } from "@/werewolf/components/hunterTool";
import { WereWolfNextTool } from "@/werewolf/components/nextTool";
import { WereWolfPlayerStateUpdater } from "@/werewolf/components/playerUpdate";
import { WereWolfTimeLimiter } from "@/werewolf/components/timeLimiter";
import { WereWolfModule } from "@/werewolf/module";
import { WereWolfRole, WereWolfStatus } from "@/werewolf/player";
import { GameMode, ItemStack } from "@minecraft/server";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { WereWolfPlayerTeleport } from "../components/playerteleporter";

export class WereWolfHunterPhase extends WereWolfModule.State {
    /**获取刚死的hunter */
    get hunter() {
        return this.context.players.find(
            (p) =>
                p.role == WereWolfRole.Hunter &&
                p.status == WereWolfStatus.justDead &&
                p.isValid &&
                !p.poistend //毒死不能开枪
        );
    }

    override onEnter(): void {
        if (!this.hunter) return this.next();
        this.addComponent(HunterTool)
            .addComponent(Timer, { initialTime: 60 })
            .addComponent(WereWolfTimeLimiter, {
                onEnd: () => this.next(),
                getPlayers: () => (this.hunter ? [this.hunter] : []),
            })
            .addComponent(WereWolfNextTool, {
                onUse: () => this.next(),

                getPlayers: () => (this.hunter ? [this.hunter] : []),
            })
            .addComponent(WereWolfPlayerTeleport);

        this.addComponents(this.context.views.hunter);
    }

    startPhase() {
        if (!this.hunter) return this.next();
        this.getComponent(Timer)?.start();
        this.getComponent(WereWolfPlayerTeleport)?.tpToSpeakingLoc(this.hunter);
        //给物品
        const item = new ItemStack("werewolf:next");
        item.nameTag = "放弃技能";
        this.hunter?.giveItem(item);
        this.hunter?.giveItem(new ItemStack("werewolf:hunter_gun"));
        //改模式
        this.hunter.player?.setGameMode(GameMode.Adventure);
    }

    next() {
        this.hunter?.clear();
        //更新死亡状态和模式
        this.addComponent(WereWolfPlayerStateUpdater);
        this.popState();
    }
}
